package cn.dylan.pet.controller;

import cn.dylan.basic.annotation.PreAuthorize;
import cn.dylan.basic.exception.BusinessException;
import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.LogininfoContext;
import cn.dylan.basic.util.PageList;

import cn.dylan.org.domain.Employee;
import cn.dylan.org.service.IEmployeeService;
import cn.dylan.pet.domain.SearchMasterMsg;
import cn.dylan.pet.dto.AcceptDto;
import cn.dylan.pet.query.SearchMasterMsgQuery;
import cn.dylan.pet.service.ISearchMasterMsgService;
import cn.dylan.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     *
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    @PreAuthorize(name = "查询所有宠物",sn = "searchMasterMsg:get")
    public List<SearchMasterMsg> loadAll(HttpServletRequest request){
        Logininfo logininfo = LogininfoContext.getLogininfo(request);
        return searchMasterMsgService.loadAll();
    }

    /**
     * GetMapping 请求
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public SearchMasterMsg loadById(@PathVariable("id") Long id){
        return searchMasterMsgService.LoadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个部门" )
    public JsonResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            searchMasterMsgService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 修改和添加
     * @param searchMasterMsg
     * @return
     */
    @PostMapping
    @ApiOperation(value = "修改或者添加" )
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if(searchMasterMsg.getId()==null){
                //添加操作
                searchMasterMsgService.save(searchMasterMsg);
            }else {
                searchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 待审核页面分页查询
     * @param query
     * @return
     */
    @PostMapping("/toAudit")
    @ApiOperation(value = "分页查询" )
    public PageList<SearchMasterMsg> QueryPage(@RequestBody SearchMasterMsgQuery query){
        System.out.println(query);
        try {
            return searchMasterMsgService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 待处理页面分页查询
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    @ApiOperation(value = "分页查询" )
    public PageList<SearchMasterMsg> QueryPage2(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LogininfoContext.getLogininfo(request);
            return searchMasterMsgService.toHandle(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            searchMasterMsgService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            Logininfo logininfo = LogininfoContext.getLogininfo(request);
            System.out.println(logininfo);
            searchMasterMsgService.publish(searchMasterMsg,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统错误，稍后重试");
        }
    }

    /**
     * 拒单
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统错误");
        }
    }
    /*this.$http.post("/searchMasterMsg/accept",
    {

    })*/

    @PostMapping("/accept")
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){
        try {
            Logininfo logininfo = LogininfoContext.getLogininfo(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统错误，稍后重试");
        }
    }


}
